package it.tref.eclipse.wicket.plugin.wizards;

import it.tref.eclipse.wicket.plugin.WicketFunActivator;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (java).
 */
public class WicketFunNewComponentWizard extends NewTypeWizardPage
{
	private static final String WICKET_PAGE = "org.apache.wicket.markup.html.WebPage";
	private static final String WICKET_PANEL = "org.apache.wicket.markup.html.panel.Panel";
	
	private Button fCreateProperties;
	private final WicketComponent component;
	
	public enum WicketComponent
	{
		Page, Panel
	}
	
	

    public WicketFunNewComponentWizard(WicketComponent component)
    {
        super(true, "WicketWizard");
        this.component = component;
    }

    /**
     * The wizard managing this wizard page must call this method
     * during initialization with a corresponding selection.
     */   
    public void init(IStructuredSelection selection)
    {
        IJavaElement jelem = getInitialJavaElement(selection);
        initContainerPage(jelem);
        initTypePage(jelem);
        doStatusUpdate();
    }

    private void doStatusUpdate()
    {
        // define the components for which a status is desired
        IStatus[] status = new IStatus[] {
            fContainerStatus,
            isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus,
            fTypeNameStatus,
        };
        updateStatus(status);
    }

    protected void handleFieldChanged(String fieldName)
    {
        super.handleFieldChanged(fieldName);
        doStatusUpdate();
    }
	
    public void createControl(Composite parent)
    {
        initializeDialogUnits(parent);
        Composite composite = new Composite(parent, SWT.NONE);
        int nColumns = 4;
        GridLayout layout = new GridLayout();
        layout.numColumns = nColumns;
        composite.setLayout(layout);
        
        setImageDescriptor(WicketFunActivator.getImageDescriptor("icons/wicket-48.png"));
        
        // Create the standard input fields
        createContainerControls(composite, nColumns);
        createPackageControls(composite, nColumns);
        createSeparator(composite, nColumns);
        createTypeNameControls(composite, nColumns);
        createSuperClassControls(composite, nColumns);
        createModifierControls(composite, nColumns);
        createSeparator(composite, nColumns);
        
        // Create the checkbox controlling whether we want stubs
        fCreateProperties = new Button(composite, SWT.CHECK);
        fCreateProperties.setText("Create '.properties' file");
        fCreateProperties.setSelection(true);
       
        GridData gd = new GridData();
        gd.horizontalSpan = nColumns;
        fCreateProperties.setLayoutData(gd);

        setControl(composite);

        // Initialize fields value
        IWorkbenchWindow aw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if(aw != null && aw.getActivePage() != null)
        {
        	IEditorPart editor = aw.getActivePage().getActiveEditor();
        	IJavaElement je = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
        	initTypePage(je);
        	initContainerPage(je);
        }
        
        if(component.equals(WicketComponent.Page))
        {
        	setTitle("Wicket Page");
        	setDescription("Create new Wicket web page");
        	setSuperClass(WICKET_PAGE, true);
        }
        else if(component.equals(WicketComponent.Panel))
        {
        	setTitle("Wicket Panel");
        	setDescription("Create new Wicket panel");
        	setSuperClass(WICKET_PANEL, true);
        }
    }

	public boolean isCreatePropertiesChecked() {
		return fCreateProperties.getSelection();
	}
}